/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ImageUtil.java   
 * @Package com.swift.maintenance.utils   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-7-22 下午2:46:29   
 * @version V1.0     
 */  
package com.swift.maintenance.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.media.ExifInterface;
import android.text.TextUtils;
import android.util.Log;

import com.swift.maintenance.base.AppConfig;

/**   
 * @ClassName:  ImageUtil   
 * @Description:TODO(这里用一句话描述这个类的作用)   
 * @author: HYM  
 * @date:   2017-7-22 下午2:46:29   
 *      
 */
public class ImageUtil {

	public static File getImageFileFromPath(String path) {

		if (TextUtils.isEmpty(path)) {
			return null;
		}
		File file = new File(path);
		String name = file.getName();

		File floders = new File(path);
		if (!floders.exists()) {
			floders.mkdirs();
		}

		File cachefile = new File(AppConfig.DIR_IMAGE, name);
		if (cachefile != null && cachefile.exists()) {
			Log.e("cachefile", "exists");
			return cachefile;
		}
		BufferedInputStream in_buff = null;
		try {
			in_buff = new BufferedInputStream(new FileInputStream(
					new File(path)));

			int i = in_buff.available() / 1024;
			if (i <= 150) {
				Log.e("size", "小于150 直接返回");
				return file;
			}
		} catch (FileNotFoundException e1) {
			return null;
		} catch (IOException e) {
			return null;
		}

		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeStream(in_buff, null, options);

		if (in_buff != null) {
			try {
				in_buff.close();
			} catch (IOException e) {
				return null;
			}
		}

		Bitmap decodeStream = null;

		double ratio = Math.max(options.outWidth * 1.0d / 1024f,
				options.outHeight * 1.0d / 1024f);

		options.inSampleSize = (int) Math.ceil(ratio);
		options.inJustDecodeBounds = false;

		BufferedInputStream in_file = null;
		try {
			 in_file = new BufferedInputStream(new FileInputStream(new
			 File(path)));
		} catch (FileNotFoundException e1) {
			return null;
		}

		decodeStream = BitmapFactory.decodeStream(in_file, null, options);

		if (decodeStream == null) {
			return null;
		}

		if (in_file != null) {
			try {
				in_file.close();
			} catch (IOException e) {
				return null;
			}
		}

		int degree = 0;
		try {
			ExifInterface exifInterface = new ExifInterface(path);
			int orientation = exifInterface.getAttributeInt(
					ExifInterface.TAG_ORIENTATION,
					ExifInterface.ORIENTATION_NORMAL);
			switch (orientation) {
			case ExifInterface.ORIENTATION_ROTATE_90:
				degree = 90;
				break;
			case ExifInterface.ORIENTATION_ROTATE_180:
				degree = 180;
				break;
			case ExifInterface.ORIENTATION_ROTATE_270:
				degree = 270;
				break;
			}
		} catch (IOException e) {
			return null;
		}
		if (degree > 0) {
			// 修正角度
			Matrix matrix = new Matrix();
			matrix.postRotate(degree);
			Bitmap resizedBitmap = Bitmap.createBitmap(decodeStream, 0, 0,
					decodeStream.getWidth(), decodeStream.getHeight(), matrix,
					true);
			// bitma转换成文件
			return compressImageFileSize(resizedBitmap, cachefile);
		} else {
			return compressImageFileSize(decodeStream, cachefile);
		}

	}
	
	/**
	 * @param resizedBitmap
	 * @param cachefile
	 * */
	public static File compressImageFileSize(Bitmap resizedBitmap,
			File cachefile) {

		FileOutputStream fileOutputStream;
		try {
			fileOutputStream = new FileOutputStream(cachefile);
			resizedBitmap.compress(CompressFormat.JPEG, 90, fileOutputStream);
			if (resizedBitmap != null && !resizedBitmap.isRecycled()) {
				resizedBitmap.recycle();
			}
			return cachefile;
		} catch (FileNotFoundException e) {
			Log.e("myUtils", "compressImageFileSize---FileNotFoundException");
			return null;
		}
	}
}
